package javacommon.base;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;

import com.google.gson.Gson;
import com.smg.community.model.ExtJsPageData;
import com.smg.community.model.SmgMember;
import com.smg.community.service.CityManager;
import com.smg.community.service.SmgGroupCategoryManager;
import com.smg.community.service.SmgGroupManager;
import com.smg.community.service.SmgGroupMemberManager;
import com.smg.community.service.SmgIndexRightbottomImageManager;
import com.smg.community.service.SmgIndexTopFiveimagesManager;
import com.smg.community.service.SmgKeywordManager;
import com.smg.community.service.SmgMemberBehaviorManager;
import com.smg.community.service.SmgMemberFocusManager;
import com.smg.community.service.SmgMemberFriendManager;
import com.smg.community.service.SmgMemberManager;
import com.smg.community.service.SmgMemberPointDetailManager;
import com.smg.community.service.SmgMemberVisitManager;
import com.smg.community.service.SmgMessageManager;
import com.smg.community.service.SmgTopicActivityMemberManager;
import com.smg.community.service.SmgTopicActivitySettingManager;
import com.smg.community.service.SmgTopicManager;
import com.smg.community.service.SmgTopicReplyManager;
import com.smg.community.setting.Constant;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yushengbing
 * 
 * @param <Entity>
 * @param <PK>
 */
/**
 * @author user
 *
 * @param <Entity>
 * @param <PK>
 */
public class BaseRestSpringController<Entity, PK> extends UrlFilenameViewController implements Serializable
{
	
	@Autowired
	public SmgIndexRightbottomImageManager smgIndexRightbottomImageManager;
	@Autowired
	public SmgIndexTopFiveimagesManager smgIndexTopFiveimagesManager;
	@Autowired
	public  CityManager cityManager;
	@Autowired
	public SmgKeywordManager smgKeywordManager;
	@Autowired
	public SmgGroupManager smgGroupManager;
	@Autowired
	public SmgTopicManager smgTopicManager;
	@Autowired
	public SmgGroupCategoryManager  smgGroupCategoryManager ;
	@Autowired
	public SmgMemberManager smgMemberManager;
	@Autowired
	public SmgTopicReplyManager smgTopicReplyManager;
	@Autowired
	public SmgTopicActivitySettingManager smgTopicActivitySettingManager;
	@Autowired
	public SmgTopicActivityMemberManager smgTopicActivityMemberManager;
	@Autowired
	public SmgMemberBehaviorManager smgMemberBehaviorManager;
	@Autowired
	public SmgGroupMemberManager smgGroupMemberManager;
	@Autowired
	public SmgMemberVisitManager smgMemberVisitManager;
	@Autowired
	public SmgMemberPointDetailManager smgMemberPointDetailManager;
	@Autowired
	public SmgMemberFocusManager smgMemberFocusManager;
	@Autowired
	public SmgMemberFriendManager smgMemberFriendManager;
	@Autowired
	public SmgMessageManager smgMessageManager;	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * mysql分页使用的是limit start
	 * */
	public void setStartAndLimit(BaseQuery query,int pageNumber)
	{
		query.setStart((pageNumber-1)* BaseQuery.DEFAULT_PAGE_SIZE);
		query.setLimit(25);
	}
	/**
	 * mysql分页使用的是limit start
	 * */
	public void setStartAndLimit(BaseQuery query,int pageNumber,int pageSize)
	{
		query.setStart((pageNumber-1)* pageSize);
		query.setLimit(  pageSize);
	}

	public void writeErrorMessage(HttpServletResponse response)
	{
		writeErrorMessage(response, null);
	}

	
	public void writeErrorMessage(HttpServletResponse response, Map<String, Object> map)
	{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		if (null == map)
		{
			map = new HashMap<String, Object>();
		}
		map.put("state", -1);
		Writer out;
		try
		{
			out = response.getWriter();
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void writeSuccessMessage(HttpServletResponse response)
	{
		writeSuccessMessage(response, null);
	}

	public void writeSuccessMessage(HttpServletResponse response, Map<String, Object> map)
	{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		if (null == map)
		{
			map = new HashMap<String, Object>();
		}
		map.put("state", 1);
		if(map.get("desc") == null){
			map.put("desc", "success");
		}
		Writer out;
		try
		{
			out = response.getWriter();
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void writeMessage(Writer out, Object map)
	{
		try
		{
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	
	public  ExtJsPageData<Entity>  transToExtJsPageData(List<Entity> resultList,int totalCount) {
		ExtJsPageData<Entity>  extJsPageData = new ExtJsPageData<Entity>();
		extJsPageData.setCount(totalCount);
		extJsPageData.setDesc("success");
		extJsPageData.setRoot(resultList);
		extJsPageData.setState(1);
		return extJsPageData;
	}
	
	public Map<String,String> writeSuccessMessage(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","1");
		map.put("desc","success");
		return map;
	}
	public Map<String,String> writeSuccessMessage(String message){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","1");
		map.put("desc",message);
		return map;
	}
	
	public Map<String,String> writeSuccessMessage(Map<String,String> map){
		if ( map == null){
			map = new HashMap<String,String>();
		}
		map.put("state","1");
		map.put("desc","success");
		return map;
	}
	
	public Map<String,String> writeErrorMessage(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","-1");
		map.put("desc","error");
		return map;
	}
	
	public Map<String,String> writeErrorMessage(String message){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","-1");
		map.put("desc",message);
		return map;
	}
	public Map<String,String> writeErrorMessage(Map<String,String> map){
		if ( map == null){
			map = new HashMap<String,String>();
		}
		map.put("state","-1");
		return map;
	}
	
	/**
	 * 设置以下公共属性：
	 * 1.当前登录用户信息
	 * @param request
	 * @param model
	 */
	public void setCommunityCommonModel(HttpServletRequest request,ModelMap model){
		
		SmgMember loginMember = getLoginMember(request);
		int unReadMessageCount = 0;
		int newFansCount  = 0;
		if ( loginMember == null){
			model.addAttribute("isLogin",false);
		}else{
			model.addAttribute("loginMember",loginMember);
			model.addAttribute("isLogin",true);
			int loginMemberId = loginMember.getMemberId();
			unReadMessageCount = smgMessageManager.getUnReadMessageCount(loginMemberId);
			 newFansCount = smgMemberFocusManager.getNewfansCount(loginMemberId);
		}
		
		model.addAttribute("unReadMessageCount",unReadMessageCount);
		model.addAttribute("newFansCount", newFansCount);
		if (  unReadMessageCount > 0 || newFansCount  > 0 ){
			model.addAttribute("isNew",true);
		}else{
			model.addAttribute("isNew",false);
		}
	}
	
	/**
	 * 设置社区公共头部上面的选定的区域
	 * 
	 * @param model
	 * @param index
	 */
	public void setHeadTabIndex(ModelMap model,int index){
		model.addAttribute("tabIndex",index);
	}
	
	/**
	 * 设置话题列表的排序方式
	 * @param orderBy
	 * @return
	 */
	public String  setQueryTopicOrderbyString(int  orderBy){
		String orderByString = "";
		if ( orderBy == 1){
			orderByString = "createDate desc";
		}else if ( orderBy == 2 ){
			orderByString = "replyCount desc";
		}else if (orderBy == 3){
			orderByString = "clickCount desc";
		}else if ( orderBy == 4){
			orderByString = "lastReplyDate desc";
		}
		return orderByString;
	}
	
	public static SmgMember getLoginMember(HttpServletRequest request){
		SmgMember loginMember = (SmgMember)request.getSession().getAttribute(Constant.SESSION_MEMBER_KEY);
		return loginMember;
	}
	
	public static  void saveMemberInSession(HttpServletRequest request,SmgMember member){
		request.getSession().setAttribute(Constant.SESSION_MEMBER_KEY,member);
	}
	
	public static void clearMemberInSession(HttpServletRequest request){ 
		request.getSession().setAttribute(Constant.SESSION_MEMBER_KEY,null);
	}
	
	public String toErrorPage(ModelMap model,String message){
		model.addAttribute("isLogin",false);
		model.addAttribute("message",message);
		return "/common/error";
	}
	public String toErrorPage(ModelMap model,String showMessage,Exception e){
		model.addAttribute("message",showMessage);
		StackTraceElement[] traces = e.getStackTrace();
		StringBuffer sb = new StringBuffer();
		for ( StackTraceElement trace:traces){
			sb.append(trace.toString());
		}
		model.addAttribute("exception",sb.toString());
		return "/common/error";
	}
	public String toErrorPage(ModelMap model ,Exception e){
		model.addAttribute("message","出错了...");
		StackTraceElement[] traces = e.getStackTrace();
		StringBuffer sb = new StringBuffer();
		for ( StackTraceElement trace:traces){
			sb.append(trace.toString());
		}
		model.addAttribute("exception",sb.toString());
		return "/common/error";
	}
	
	public String toUnloginPage(HttpServletRequest request){
		return null;
	}
}
